//http://bj.58.com/ershoujiaju/
phantom.casperPath = 'D:\\phantomjs\\casperjs\\';
phantom.injectJs(phantom.casperPath + 'bin\\bootstrap.js');
//phantom.injectJs('jquery-1.7.2.js');
phantom.injectJs('util\\cutil.js');
phantom.injectJs("D:\\phantomjs\\js\\json.js");

var casper = require('casper').create();

if (phantom.args.length === 0) {
    console.log('Usage: cartersjson.js <some URL>');
    phantom.exit();
} 
address = phantom.args[0];

casper.on('load.finished', function() {
    var page = this.page;
});

function getbaseInfo(array) {
   
    var a = $("#infolist tr td:has(i) > a");

    for (var i = 0; i < a.length; i++) {
        var url = a[i].href;
        var title = a[i].textContent.trim();
        var jsonData = {
            "title" : "",
            "url" : ""
        };
        jsonData.title = title;
        jsonData.url = url;
        array.push(jsonData);
    }

    return array;
}

casper.start();
//console.log('####### FETCH #######' + address);
casper.thenOpen(address, function() {
    //console.log("start entry");
    var page = this.page;
    var array = new Array();
    
    array = this.evaluate(getbaseInfo, {
        array : array
    });

    console.log(JSON.stringify(array));
});


casper.run();

/** trim() method for String */
String.prototype.trim=function() {
	return this.replace(/(^\s*)|(\s*$)/g,'');
};

//http://bj.58.com/ershoujiaju/16601651416710x.shtml
